package com.kaifamiao.ajax.dao.impl;

import com.kaifamiao.ajax.dao.StudentDao;
import com.kaifamiao.ajax.entity.Student;
import com.kaifamiao.ajax.exception.DataAccessException;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl extends BaseDao<Student, Long> implements StudentDao {

    private static final String INSERT = "INSERT INTO t_students (name, gender, birthdate, hometown, id) VALUES (?, ?, ?, ?, ?)";
    private static final String UPDATE = "UPDATE t_students SET name=?, gender=?, birthdate=?, hometown=? WHERE id = ?";
    private static final String DELETE = "DELETE FROM t_students WHERE id = ?";
    private static final String QUERY_ALL = "SELECT id, name, gender, birthdate, hometown FROM t_students ORDER BY 1";
    private static final String QUERY_BY_ID = "SELECT id, name, gender, birthdate, hometown FROM t_students WHERE id = ?";

    private static final ResultSetHandler<List<Student>> rsHandler = rs -> {
        List<Student> list = new ArrayList<>();
        while ( rs.next() ) {
            Student s = new Student();
            s.setId( rs.getLong("id") );
            s.setName( rs.getString("name") );
            s.setGender( rs.getString("gender") );
            java.sql.Date sqlDate = rs.getDate( "birthdate" );
            java.time.LocalDate birthdate = sqlDate == null ? null : sqlDate.toLocalDate();
            s.setBirthdate(birthdate);
            s.setHometown( rs.getString("hometown") );
            list.add(s);
        }
        return list;
    };

    @Override
    public boolean save(Student s) {
        java.time.LocalDate birthdate = s.getBirthdate();
        java.sql.Date sqlDate = birthdate == null ? null : java.sql.Date.valueOf(birthdate);
        try {
            int count = runner.update(INSERT, s.getName(), s.getGender(), sqlDate, s.getHometown(), s.getId() );
            return count == 1 ;
        } catch ( SQLException cause ) {
            throw new DataAccessException( "保存失败", cause);
        }
    }

    @Override
    public boolean update(Student s) {
        java.time.LocalDate birthdate = s.getBirthdate();
        java.sql.Date sqlDate = birthdate == null ? null : java.sql.Date.valueOf(birthdate);
        try {
            int count = runner.update(UPDATE, s.getName(), s.getGender(), sqlDate, s.getHometown(), s.getId() );
            return count == 1 ;
        } catch ( SQLException cause ) {
            throw new DataAccessException( "更新失败", cause);
        }
    }

    @Override
    public boolean remove(Long id) {
        try {
            int count = runner.update(DELETE, id );
            return count == 1 ;
        } catch ( SQLException cause ) {
            throw new DataAccessException( "删除失败", cause);
        }
    }

    @Override
    public List<Student> findAll() {
        try {
            List<Student> list = runner.query(QUERY_ALL, rsHandler);
            return list;
        } catch ( SQLException cause ) {
            throw new DataAccessException( "查询失败", cause);
        }
    }

    @Override
    public Student find(Long id) {
        try {
            List<Student> list = runner.query(QUERY_BY_ID, rsHandler, id );
            if( list.size() > 0 ) {
                return list.get(0);
            }
            return null;
        } catch ( SQLException cause ) {
            throw new DataAccessException( "查询失败", cause);
        }
    }

}
